Skip to content

Latest commit

 

History

History
429 lines (255 loc) · 7.32 KB

File metadata and controls

429 lines (255 loc) · 7.32 KB

Class SdFatFile

Description

Defines a class of SD FAT File.

Syntax
class SdFatFile

Members

Public Constructors  
SdFatFile::SdFatFile Constructs a SdFatFile object
SdFatFile::~SdFatFile Destructs a SdFatFile object
Public Methods  
SdFatFile::write Write 1 byte/bytes to file
SdFatFile::read Read 1 byte/bytes from the file
SdFatFile::peek Read 1 byte from file without move curser
SdFatFile::available Check if the cursor is at EOF (End-Of-File)
SdFatFile::bool Check if file is opened
SdFatFile::seek Change cursor to a specific position
SdFatFile::close Close file

SdFatFile::write

Description
Write 1 byte or bytes to the file.
Syntax
size_t SdFatFile::write(uint8_t c);
size_t SdFatFile::write(const uint8_t *buf, size_t size);
Parameters
c: The character to be written.
buf: The buffer to be written.
size: The length of buffer to be written.
Returns
The function returns the number of byte count that has been successfully written to the file.
Example Code
NA.
Notes and Warnings
Include “SdFatFile.h” to use the class function.

SdFatFile:: read

Description
Read 1 byte or bytes from the file.
Syntax
int SdFatFile::read(void);
int SdFatFile::read(void *buf, uint16_t nbyte);
Parameters
buf: The buffer to store the content.
nbyte: The buffer size. (Or can be regarded as the desired length to read).
Returns
The function returns a read character or the read size of the buffer.

Example Code

  1. #include "FatFs_SD.h"
  2. char dirname[] = "testdir";
  3. char filename[] = "test.txt";
  4. char write_content[] = "hello world!";
  5. FatFsSD fs;
  6. void setup() {
  7. char buf[128];
  8. char absolute_filename[128];
  9. fs.begin();
  10. sprintf(absolute_filename, "%s%s", fs.getRootPath(), dirname);
  11. fs.mkdir(absolute_filename);
  12. printf("create dir at \"%s"rn", absolute_filename);
  13. sprintf(absolute_filename, "%s%s/%s", fs.getRootPath(), dirname, filename);
  14. SdFatFile file = fs.open(absolute_filename);
  15. file.println(write_content);
  16. file.close();
  17. printf("create file at \"%s"rn", absolute_filename);
  18. printf("read back from \"%s"rn", absolute_filename);
  19. file = fs.open(absolute_filename);
  20. memset(buf, 0, sizeof(buf));
  21. file.read(buf, sizeof(buf));
  22. file.close();
  23. printf("==== content ====rn");
  24. printf("%s", buf);
  25. printf("==== end ====rn");
  26. fs.end();
  27. }
  28. void loop() {
  29. delay(1000);
  30. }
Example: create_folder;
This example shows how to create a folder and open a file under it.
  1. #include "FatFs_SD.h"
  2. char filename[] = "test.txt";
  3. char write_content[] = "hello world!";
  4. FatFsSD fs;
  5. void setup() {
  6. char buf[128];
  7. char absolute_filename[128];
  8. fs.begin();
  9. printf("write something to \"%s"rn", filename);
  10. sprintf(absolute_filename, "%s%s", fs.getRootPath(), filename);
  11. SdFatFile file = fs.open(absolute_filename);
  12. file.println(write_content);
  13. file.close();
  14. printf("write finishrnrn");
  15. printf("read back from \"%s"rn", filename);
  16. file = fs.open(absolute_filename);
  17. memset(buf, 0, sizeof(buf));
  18. file.read(buf, sizeof(buf));
  19. file.close();
  20. printf("==== content ====rn");
  21. printf("%s", buf);
  22. printf("==== end ====rn");
  23. fs.end();
  24. }
  25. void loop() {
  26. delay(1000);
  27. }
Example: file_read_write;
This example shows how to open/close files and perform read/write to it.
Notes and Warnings
Include “SdFatFile.h” to use the class function.

SdFatFile:: peek

Description
Read one byte from the file without moving the curser.
Syntax
int SdFatFile::peek(void);
Parameters
The function requires no input parameter.
Returns
The function returns the read character as an integer number.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.

SdFatFile:: available

Description
Check if the cursor is at EOF.
Syntax
int SdFatFile::available(void);
Parameters
The function requires no input parameter.
Returns
The function returns “0” if the cursor is at EOF, else returns “1”.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.

SdFatFile:: flush

Description
It is a nop. This is an inherited function from class Stream. And it does not affect SD File.
Syntax
void SdFatFile::flush(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.

SdFatFile:: seek

Description
Change cursor to a specific position.
Syntax
int SdFatFile::seek(uint32_t pos);
Parameters
pos: The desired position.
Returns
The function returns 0 if success otherwise returns a negative value.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” in order to use the class function.

SdFatFile:: close

Description
Close file.
Syntax
int SdFatFile::close(void);
Parameters
The function requires no input parameter.
Returns
The function returns 0 if runs successfully otherwise it returns a negative value.
Example Code
Example: last_modified_time;
The example shows how to get and set last modified time of a file.
Example: create_folder;
This example shows how to create a folder and open a file under it. The details of the code can be found in the section of SdFatFile:: read.
Example: file_read_write;
This example shows how to open/close files and perform read/write to it. The details of the code can be found in the section of SdFatFile:: read.
  1. #include <FatFs_SD.h>
  2. FatFsSD fs;
  3. char filename[] = "test.txt";
  4. void setup() {
  5. char absolute_filename[128];
  6. uint16_t year = 2021;
  7. uint16_t month = 4;
  8. uint16_t date = 4;
  9. uint16_t hour = 12;
  10. uint16_t minute = 12;
  11. uint16_t second = 12;
  12. fs.begin();
  13. sprintf(absolute_filename, "%s%s", fs.getRootPath(), filename);
  14. SdFatFile file = fs.open(absolute_filename);
  15. file.close();
  16. fs.setLastModTime(absolute_filename, year, month, date, hour, minute, second);
  17. fs.getLastModTime(absolute_filename, &year, &month, &date, &hour, &minute, &second);
  18. printf("filename:"%s"rn", absolute_filename);
  19. printf("time mod:%04d/%02d/%02d %02d:%02d:%02drn", year, month, date, hour, minute, second);
  20. fs.end();
  21. }
  22. void loop() {
  23. delay(1000);
  24. }
Notes and Warnings
Include “SdFatFile.h” in order to use the class function.